<?php
/**
 * 单页
 */
namespace App\Service;

use App\Models\EwFeedbacks;

class FeedbackService  extends BaseService
{
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['f.id,f.member_id,m.nickname,u.mobile,u.account,f.content,f.contact,f.create_time']);
		$builder->addFrom("App\Models\EwFeedbacks", 'f');
		$builder->leftJoin("App\Models\EwMember", 'f.member_id = m.id' , 'm');
		$builder->leftJoin("App\Models\EwUser", 'f.member_id = u.id' , 'u');

		//mobile
		if (isset($params->mobile) && $params->mobile)
			$builder->andwhere("u.mobile LIKE '%" . $params->mobile . "%'");
		
		//account
		if (isset($params->account) && $params->account)
			$builder->andwhere("u.account LIKE '%" . $params->account . "%'");

		//nickname
		if (isset($params->nickname) && $params->nickname)
			$builder->andwhere("m.nickname LIKE '%" . $params->nickname . "%'");

		//content
		if (isset($params->content) && $params->content)
			$builder->andwhere("f.content LIKE '%" . $params->content . "%'");

		//contact
		if (isset($params->contact) && $params->contact)
			$builder->andwhere("f.contact LIKE '%" . $params->contact . "%'");
			
		if ($count)
			return $builder->getQuery()->execute()->count();

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();

		return $result->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwFeedbacks::findFirst($id);
	}

	public static function count()
	{
		return EwFeedbacks::count();
	}
	
	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwFeedbacks;
		if (isset($data->id) && $data->id)
			$row = EwFeedbacks::findFirst($data->id);

		return $row->save((array)$data);
	}
	
}